package com.tuangou.merchant.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.tuangou.common.utils.DateUtils;
import com.tuangou.merchant.domain.EmailLogin;
import com.tuangou.merchant.result.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;
import com.tuangou.merchant.mapper.EmployeeMapper;
import com.tuangou.merchant.domain.Employee;
import com.tuangou.merchant.service.IEmployeeService;

import javax.annotation.Resource;

/**
 * 员工信息Service业务层处理
 * 
 * @author wxg
 * @date 2025-05-11
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService
{
    @Autowired
    private EmployeeMapper employeeMapper;


    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    //发送邮箱
    private MailSender mailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    @Override
    public Result sendEmail(String email) {
        SimpleMailMessage smg = new SimpleMailMessage();

        EmailLogin emailLogin =employeeMapper.getByEmail(email);
        if (emailLogin==null){
            return Result.error("用户不存在");
        }
        //随机六位数验证码
        String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        smg.setText("验证码："+code);
        smg.setFrom(fromEmail);
        smg.setTo(email);
        smg.setSubject("登入验证码");
        redisTemplate.opsForValue().set(email,code,60, TimeUnit.SECONDS);
        mailSender.send(smg);


        return Result.success();

    }

    //邮箱登入
    @Override
    public Result emailLogin(String email, String code) {
        EmailLogin employeeEmail = employeeMapper.getByEmail(email);
        if (employeeEmail==null){
            return Result.error("用户不存在");
        }
        String redisCode = (String) redisTemplate.opsForValue().get(email);
//       if (!redisTemplate.hasKey(redisCode)){
//           return Result.error("key已经过期或者不存在");
//       }
        if (!redisCode.equals(code)){
            return Result.error("验证码不正确，请重新输入");
        }




        return Result.success();
    }












    /**
     * 查询员工信息
     * 
     * @param id 员工信息主键
     * @return 员工信息
     */
    @Override
    public Employee selectEmployeeById(Long id)
    {
        return employeeMapper.selectEmployeeById(id);
    }

    /**
     * 查询员工信息列表
     * 
     * @param employee 员工信息
     * @return 员工信息
     */
    @Override
    public List<Employee> selectEmployeeList(Employee employee)
    {
        return employeeMapper.selectEmployeeList(employee);
    }

    /**
     * 新增员工信息
     * 
     * @param employee 员工信息
     * @return 结果
     */
    @Override
    public int insertEmployee(Employee employee)
    {
        employee.setCreateTime(DateUtils.getNowDate());
        return employeeMapper.insertEmployee(employee);
    }

    /**
     * 修改员工信息
     * 
     * @param employee 员工信息
     * @return 结果
     */
    @Override
    public int updateEmployee(Employee employee)
    {
        employee.setUpdateTime(DateUtils.getNowDate());
        return employeeMapper.updateEmployee(employee);
    }

    /**
     * 批量删除员工信息
     * 
     * @param ids 需要删除的员工信息主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeByIds(Long[] ids)
    {
        return employeeMapper.deleteEmployeeByIds(ids);
    }

    /**
     * 删除员工信息信息
     * 
     * @param id 员工信息主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeById(Long id)
    {
        return employeeMapper.deleteEmployeeById(id);
    }
}
